package utils

type SubscribeCallback = func(sender interface{}, channelid string, subscribeid interface{}, data interface{}, tag interface{}, isBreak *bool) bool

type ISubscribe interface {
	SubscribeChannel(channelid string, id interface{}, cb SubscribeCallback)
}

type ISubscribeEx interface {
	SubscribeChannelEx(channelid string, id interface{}, onInput SubscribeCallback) (subtopic string, err error)
}

type IUnSubscribe interface {
	UnsubscribeChannel(channelid string, id interface{}) bool
}

type IGetSubscriber interface {
	GetSubscriber() *SubscribeCenter
}

func TrySubscribeChannel(obj interface{}, channelid string, id interface{}, cb SubscribeCallback) bool {
	if sc, ok := obj.(ISubscribe); ok {
		sc.SubscribeChannel(channelid, id, cb)
		return true
	}

	if sc, ok := obj.(*SubscribeCenter); ok {
		sc.SubscribeChannel(channelid, id, cb)
		return true
	}

	if sc, ok := obj.(IGetSubscriber); ok {
		sc.GetSubscriber().SubscribeChannel(channelid, id, cb)
		return true
	}

	return false
}

func TryUnSubscribe(obj interface{}, channelid string, id interface{}) bool {
	if sc, ok := obj.(IUnSubscribe); ok {
		sc.UnsubscribeChannel(channelid, id)
		return true
	}

	if sc, ok := obj.(*SubscribeCenter); ok {
		sc.UnsubscribeChannel(channelid, id)
		return true
	}

	if sc, ok := obj.(IGetSubscriber); ok {
		sc.GetSubscriber().UnsubscribeChannel(channelid, id)
		return true
	}

	return false
}
